package com.xwaiy.xwaiyrpc.proxy;

import com.xwaiy.xwaiyrpc.RpcApplication;

import java.lang.reflect.Proxy;

/**
 * 服务代理工厂
 * @Author Xwaiy
 * @Date 2025/8/21 16:36
 **/
public class ServiceProxyFactory {
    /**
     * 根据服务类型获取代理对象
     * @param serviceClass
     * @return
     * @param <T>
     */
    public static <T> T getProxy(Class<T> serviceClass) {
        if(RpcApplication.getRpcConfig().isMock()){
            return  getMockProxy(serviceClass);
        }

        return (T) Proxy.newProxyInstance(
                serviceClass.getClassLoader(),
                new Class[]{serviceClass},
                new ServiceProxy());
    }

    public static <T> T getMockProxy(Class<T> serviceClass) {
        return (T) Proxy.newProxyInstance(
                serviceClass.getClassLoader(),
                new Class[]{serviceClass},
                new MockServiceProxy());
    }
}
